package com.itsv.news.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class ConectionUtil {
	private static final String APPLICATION_JSON = "application/json";

	private static final String CONTENT_TYPE_TEXT_JSON = "text/json";

	private static String content;

	public static String getJson(String request) throws Exception {
//		String url = "http://192.168.0.148:8080/iChyThePopulation.controller1";  // 内网IP
		String url = "http://192.168.0.227:8080/cmsnew2012qt/rs/jerseyService/getArticlesNextPageno";  // 外网IP
		String json = request;
		String encoderJson = json;

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);

		StringEntity se;
		se = new StringEntity(encoderJson);
		se.setContentType(CONTENT_TYPE_TEXT_JSON);
		se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
				APPLICATION_JSON));

		httpPost.setEntity(se);

		HttpResponse httpResponse = httpClient.execute(httpPost);

		byte[] bytes = EntityUtils.toByteArray(httpResponse.getEntity());
		content = new String(bytes, "UTF-8");
		return content;
	}
}
